using System;
using System.Collections.Generic;
using System.Text;

namespace MapMaker.VirtualEarth
{
    public class Tile
    {
        public int LevelOfDetail;
        public int X;
        public int Y;
        private string quadKey = null;

        public Tile(int x, int y, int levelOfDetail)
        {
            this.LevelOfDetail = levelOfDetail;
            this.X = x;
            this.Y = y;
        }

        public Tile()
        {

        }

        public string QuadKey
        {
            get
            {
                if (quadKey == null)
                {
                    quadKey = VirtualEarthTileSystem.TileXYToQuadKey(X, Y, LevelOfDetail);
                }

                return quadKey;
            }
        }

        public bool Contains(Tile tile)
        {
            bool contains = false;

            if (tile.LevelOfDetail >= this.LevelOfDetail)
            {
                int tileWidth = (int)Math.Pow(2, (tile.LevelOfDetail - this.LevelOfDetail));
                int parentLowerX = this.X * (int)Math.Pow(2, tile.LevelOfDetail - this.LevelOfDetail);
                int parentUpperX = parentLowerX + tileWidth;
                int parentLowerY = this.Y * (int)Math.Pow(2, tile.LevelOfDetail - this.LevelOfDetail);
                int parentUpperY = parentLowerY + tileWidth;

                if (tile.X >= parentLowerX && tile.X < parentUpperX &&
                    tile.Y >= parentLowerY && tile.Y < parentUpperX)
                {
                    contains = true;
                }
            }

            return contains;
        }
    }
}
